/**
 * 
 */
package com.manage.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.auth.model.Account;
import com.manage.model.AppSdk;
import com.manage.config.Config;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class AppSdkServiceImpl extends AbstractService implements AppSdkService
{
	private Logger logger;
	
	public AppSdkServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.SECTION, Config.KEY_DBCP_NAME);
	}
		
	@Override
	public List<AppSdk> getAppSdks()
	{
		String sql = "select * from appsdk order by sdksort desc";
		logger.debug("sql: " + sql);
		ModelGenerator<AppSdk> dbcpmg = new ModelGenerator<AppSdk>(AppSdk.class);
		return dbcpmg.generateList(dbcp, sql);
	}
	
	@Override
	public boolean saveAppSdk(Account operator, String type, List<AppSdk> list,String appnumber)
	{
		if (operator == null || operator.getId() == null || operator.getId().length() == 0)
			return false;
		String  [] sql = new String[1+list.size()];
		
		sql[0]="delete from appsdk where appnumber='"+appnumber+"'";
		
		for(int i=0;i<list.size();i++)
		{
			AppSdk model = list.get(i);
			model.setId(getTableId("appsdk"));
			sql[i+1]="insert into appsdk (id,cpid,appnumber,sdkid,sdksort) " +
				" values ('"+model.getId()+"','"+model.getCpid()+"','"+model.getAppnumber()
				+"','"+model.getSdkid()+"','"+model.getSdksort()+"')";
		}
		return ConnectionManager.getInstance().executeBatch(dbcp, sql);
	}
}
